package com.codedancer.campus.mapper;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.ContactAdminChatMessage;
import com.codedancer.campus.qo.contactAdmin.ContactAdminChatMessagePageSearchQo;

import java.util.List;

public interface ContactAdminChatMessageMapper extends BaseMapper<ContactAdminChatMessage> {
    /**
     * 获取聊天记录
     *
     * @param contactAdminChatMessagePage
     * @param contactAdminChatMessagePageSearchQo
     * @return
     */
    default IPage<ContactAdminChatMessage> pageHistory(Page<ContactAdminChatMessage> contactAdminChatMessagePage, ContactAdminChatMessagePageSearchQo contactAdminChatMessagePageSearchQo) {
        LambdaQueryWrapper<ContactAdminChatMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ContactAdminChatMessage::getContactAdminChatRoomUuid, contactAdminChatMessagePageSearchQo.getChatRoomUuid());
        lambdaQueryWrapper.orderByAsc(ContactAdminChatMessage::getCreateTime);
        return selectPage(contactAdminChatMessagePage, lambdaQueryWrapper);
    }

    /**
     * 查询最近一次聊天信息
     *
     * @param uuid
     * @return
     */
    default ContactAdminChatMessage latestChatMessageByRoomId(String uuid) {
        LambdaQueryWrapper<ContactAdminChatMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ContactAdminChatMessage::getContactAdminChatRoomUuid, uuid);
        lambdaQueryWrapper.orderByDesc(ContactAdminChatMessage::getCreateTime);
        List<ContactAdminChatMessage> contactAdminChatMessages =
            selectList(lambdaQueryWrapper);
        return CollUtil.isEmpty(contactAdminChatMessages) ? null : contactAdminChatMessages.get(0);
    }
}
